package com.example.myzhugezhaofang.fragment.consultingfrag;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.myzhugezhaofang.R;
import com.example.myzhugezhaofang.adapter.ConsultingAdapter;
import com.example.myzhugezhaofang.bean.ConsultingBean;
import com.google.gson.Gson;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * 作者：朱勇闯
 * data : 2021/4/16  11:42
 * 作用： 资讯下市场百科
 */
public class MarketDynamicChildFragment1 extends Fragment {
    private static final String TAG = "朱勇闯MarketDynamicChild1";
    private List<ConsultingBean.ListBean> list;
    private HashMap<String, Object> wikipediaMap;
    private ConsultingAdapter consultingAdapter;
    private String secondModular;
    private RecyclerView rv_list;
    private SmartRefreshLayout smart;
    private Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(@NonNull Message msg) {
            ConsultingBean bean = (ConsultingBean) msg.obj;
            List<ConsultingBean.ListBean> datas = bean.getList();
            Log.e(TAG, "initEvent datas: " + datas);
            list.addAll(datas);
            consultingAdapter.notifyDataSetChanged();
            return false;
        }
    });

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View inflate = inflater.inflate(R.layout.fragment_consulting_item, null);
        Bundle arguments = getArguments();
        secondModular = arguments.getString("url");
        Log.e(TAG, "initEvent: url" + secondModular);
        list = new ArrayList<>();
//        wikipediaMap = new HashMap<>();
//        wikipediaMap.put("city", "bj");
//        wikipediaMap.put("modular", "zgzb");
//        wikipediaMap.put("secondModular", secondModular);
//        wikipediaMap.put("pageLimit", 20);
        String path = "https://api.zhuge.com/contentService/v1/News/appNewsList?city=bj&modular=zgzb&secondModular=" + secondModular + "&pageLimit=20";
        initData(path);
        initView(inflate);
        return inflate;
    }

    private void initView(View inflate) {
        rv_list = inflate.findViewById(R.id.rv_consulting_item);
        smart = inflate.findViewById(R.id.smart_consulting_item);
        rv_list.setLayoutManager(new LinearLayoutManager(getContext()));
        consultingAdapter = new ConsultingAdapter(list, getContext(), R.layout.item_consulting);
        rv_list.setAdapter(consultingAdapter);
    }


    private void initData(String path) {
        OkHttpClient okHttpClient = new OkHttpClient();
        Request request = new Request
                .Builder()
                .get()
                .url(path)
                .build();
        Call call = okHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.e(TAG, "onFailure: " + e.getMessage());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String json = response.body().string();
                ConsultingBean dBean = new Gson().fromJson(json, ConsultingBean.class);
                Message message = new Message();
                message.obj = dBean;
                handler.sendMessage(message);
            }
        });
    }
}

